package com.ziyun.erp.web.modules.systemConfig.controller;

import com.cloud.bus.core.EventBusPusher;
import com.cloud.bus.event.ucenter.CleanCacheEvent;
import com.service.cache.constants.CachePrefix;
import com.service.cache.constants.RegionPrefix;
import com.service.cache.core.CacheService;
import com.utility.StringTool;
import com.ziyun.erp.modules.systemConfig.entity.SystemConfigEntity;
import com.ziyun.erp.modules.systemConfig.service.SystemConfigService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



 //系统基础配置表
 // @author jiangli


@RestController
@RequestMapping("systemConfig/systemconfig")
public class SystemConfigController {
	
    @Autowired
    private SystemConfigService systemConfigService;
    @Autowired
	private CacheService cacheService;

   // 列表
    @RequestMapping("/list")
    @RequiresPermissions("systemConfig:systemconfig:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = systemConfigService.queryPage(params);

        return R.ok().put("page", page);
    }

    // 信息
    @RequestMapping("/info/{id}")
    @RequiresPermissions("systemConfig:systemconfig:info")
    public R info(@PathVariable("id") Integer id) {
        SystemConfigEntity systemConfig = systemConfigService.selectById(id);

        return R.ok().put("systemConfig", systemConfig);
    }

  // 保存
    @RequestMapping("/save")
    @RequiresPermissions("systemConfig:systemconfig:save")
    public R save(@RequestBody SystemConfigEntity systemConfig) {
        systemConfigService.insert(systemConfig);
        return R.ok();
    }

   // 修改
    @RequestMapping("/update")
    @RequiresPermissions("systemConfig:systemconfig:update")
    public R update(@RequestBody SystemConfigEntity systemConfig) {
        ValidatorUtils.validateEntity(systemConfig);
        systemConfigService.updateAllColumnById(systemConfig);//全部更新
        String key = StringTool.buildString(CachePrefix.systemConfig, String.valueOf(systemConfig.getId()));
        cacheService.removeFromCache(key, RegionPrefix.one_day);
        cacheService.removeFromCache("system_configs_list", RegionPrefix.one_day);
        return R.ok();
    }

   // 删除
    @RequestMapping("/delete")
    @RequiresPermissions("systemConfig:systemconfig:delete")
    public R delete(@RequestBody Integer[] ids) {
        systemConfigService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    //清除静态参数配置缓存
    @RequestMapping("/systemConfig")
    public R systemConfig() {
        CleanCacheEvent event = CleanCacheEvent.builder().source(this).key(CachePrefix.systemConfigsList).build();

        EventBusPusher.syncPost(event);
        return R.ok();
    }

    //清除版本配置缓存
    @RequestMapping("/systemVersion")
    public R systemVersion() {
        CleanCacheEvent event = CleanCacheEvent.builder().source(this).key(CachePrefix.systemVersions).build();

        EventBusPusher.syncPost(event);
        return R.ok();
    }

    //清除注册渠道来源缓存
    @RequestMapping("/register")
    public R register() {
        CleanCacheEvent event = CleanCacheEvent.builder().source(this).key(CachePrefix.registerSource).build();

        EventBusPusher.syncPost(event);
        return R.ok();
    }
}
